/***********************************************************************
 * Module:  Operation.java
 * Author:  Vanil
 * Purpose: Defines the Class Operation
 ***********************************************************************/
/***********************************************************************
 * Module:  MedicalProcess.java
 * Author:  Vanil
 * Purpose: Defines the Class MedicalProcess
 ***********************************************************************/
package process;
import db.dao.*;
import db.dto.*;
import db.exceptions.*;
import db.factory.DoctorHasOperatinsDaoFactory;
import db.factory.OperationDaoFactory;
import db.factory.PrescriptionDaoFactory;
import db.jdbc.*;
import personal.*;

import java.sql.Date;
import java.util.ArrayList;
import java.util.List;

public class Operation{
   OperationDto dto;
   OperationDao dao;

    public Operation(OperationDto dto) {
        this.dto = dto;
        this.dao = OperationDaoFactory.create();
    }


   public java.util.Collection<Doctor> getDoctors() throws DoctorHasOperatinsDaoException, DoctorDaoException {
        List<Doctor> list = new ArrayList<Doctor>();
        DoctorDao docDao = new DoctorDaoImpl();
        DoctorHasOperationsDao docOperDao = DoctorHasOperatinsDaoFactory.create();
        DoctorHasOperatins[] operatinDtos = docOperDao.findByOperation(this.dto.getIdOperation());
        for(DoctorHasOperatins operDto :operatinDtos)
        {
            int idDoctor = operDto.getIdDoctor();
            DoctorDto doctorDto = docDao.findByPrimaryKey(idDoctor);
            list.add(new Doctor(doctorDto));
        }
        return list;
   }

   /**
     * @param newDoctor */
   public void addDoctor(Doctor newDoctor) throws DoctorHasOperatinsDaoException {
       DoctorHasOperationsDao docOperDao = new DoctorHasOperatinsDaoImpl();
       DoctorHasOperatins operatins = new DoctorHasOperatins(newDoctor.getIdPerson(),this.dto.getIdOperation());
       docOperDao.insert(operatins);
   }
   
   /**
     * @param oldDoctor */
   public void removeDoctor(Doctor oldDoctor) throws DoctorHasOperatinsDaoException {
       DoctorHasOperationsDao docOperDao = new DoctorHasOperatinsDaoImpl();
       docOperDao.delete(dto.getIdOperation(),oldDoctor.getIdPerson());
   }

   public java.util.Collection<Nurse> getNurses() throws NurseHasOperatinsDaoException, NurseDaoException {
   List<Nurse> list = new ArrayList<Nurse>();
        NurseDao nurseDao = new NurseDaoImpl();
        NurseHasOperationsDao nurseOperDao = new NurseHasOperatinsDaoImpl();
        NurseHasOperatins[] operatinDtos = nurseOperDao.findByOperation(this.dto.getIdOperation());
        for(NurseHasOperatins operDto :operatinDtos)
        {
            int idNurse = operDto.getIdNurse();
            NurseDto nurseDto = nurseDao.findByPrimaryKey(idNurse);
            list.add(new Nurse(nurseDto));
        }
        return list;
   }

   /**
     * @param newNurse */
   public void addNurse(Nurse newNurse) throws NurseHasOperatinsDaoException {
       NurseHasOperationsDao nurseOperDao = new NurseHasOperatinsDaoImpl();
       NurseHasOperatins operatins = new NurseHasOperatins(newNurse.getIdPerson(),this.dto.getIdOperation());
       nurseOperDao.insert(operatins);
   }
   
   /**
     * @param oldNurse */
   public void removeNurse(Nurse oldNurse) throws NurseHasOperatinsDaoException {
       NurseHasOperationsDao nurseOperDao = new NurseHasOperatinsDaoImpl();
       nurseOperDao.delete(this.dto.getIdOperation(),oldNurse.getIdPerson());
   }
    public Prescription getPrescription() throws PrescriptionDaoException {
        PrescriptionDao dao = PrescriptionDaoFactory.create();
        PrescriptionDto prescriptionDto = dao.findByPrimaryKey(dto.getIdPrescription());
        return new Prescription(prescriptionDto);
    }
    public Date getDate()
    {
        return new Date(dto.getDateofoperation());
    }
    public int getCabinetNumber()
    {
        return dto.getCabinetnumber();
    }
    public String getName()
    {
        return this.dto.getName();
    }


}